package com.qianfeng.lingshijia;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

import fragment.HomeFragment;
import fragment.PersonFragment;
import fragment.SchoolMarketFragment;
import fragment.ShoppingSortFragment;

@ContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @ViewInject(R.id.rg_menu)
    private RadioGroup mRadioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        initView();
    }

    private void initView() {
        //menu设置监听
        mRadioGroup.setOnCheckedChangeListener(this);
        //进入主界面，默认选择home
        RadioButton mRadioButton_home = (RadioButton) findViewById(R.id.rb_home);
        mRadioButton_home.performClick();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_home:
                fragmentManager(R.id.fragment, HomeFragment.class);
                break;
            case R.id.rb_school_market:
                fragmentManager(R.id.fragment, SchoolMarketFragment.class);
                break;
            case R.id.rb_shoppint_cart:
                fragmentManager(R.id.fragment, ShoppingSortFragment.class);
                break;
            case R.id.rb_person:
                fragmentManager(R.id.fragment, PersonFragment.class);
                break;
        }
    }

    /**
     * 单击跳转到选择大学的界面
     *
     * @param view
     */
    public void schoolSelect(View view) {
        startActivityForResult(new Intent(this, SchoolSelectActivity.class), 100);
    }

    /**
     * 从选择大学界面出来的回调方法
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != -1) {
            if (data != null) {
                SchoolMarketFragment smFragment = (SchoolMarketFragment) getSupportFragmentManager().findFragmentByTag(SchoolMarketFragment.class.getName());
                smFragment.setSchoolNameIdAndLoadDatas(data.getStringExtra("schoolName"), resultCode);
            }
        }
    }
}
